import 'package:xbaistack/common/global_context.dart';

final class StatusCode {
  // 值是随便定义的，只要值不相同就可以随便定义
  // 不过最好是和 Http 状态码区分开
  static const int UNKNOWN = 0;
  static const int CANCEL_REQUEST = 1;
  static const int DOMAIN_ERROR = 2;
  static const int NETWORK_ERROR = 3;
  static const int SEND_TIMEOUT = 4;
  static const int CONNECTION_TIMEOUT = 5;
  static const int RECEIVE_TIMEOUT = 6;
  static const int BAD_CERTIFICATE = 7;

  /// 通过状态码获取对应的国际化文本
  static String? getLabel(int code) => _codes[code]?.call();

  /// 由于 L10N 没办法像 Map 一样通过 Key 去获取值
  /// 并且无法使用反射去读取实例的属性值，因此只能使用 Map 进行值映射了
  static final _codes = <int, String Function()>{
    UNKNOWN: () => GlobalContext.l10n.err000, // UNKNOWN
    CANCEL_REQUEST: () => GlobalContext.l10n.err001, // CANCEL_REQUEST
    DOMAIN_ERROR: () => GlobalContext.l10n.err002, // DOMAIN_ERROR
    NETWORK_ERROR: () => GlobalContext.l10n.err003, // NETWORK_ERROR
    SEND_TIMEOUT: () => GlobalContext.l10n.err004, // SEND_TIMEOUT
    CONNECTION_TIMEOUT: () => GlobalContext.l10n.err005, // CONNECTION_TIMEOUT
    RECEIVE_TIMEOUT: () => GlobalContext.l10n.err006, // RECEIVE_TIMEOUT
    BAD_CERTIFICATE: () => GlobalContext.l10n.err007, // BAD_CERTIFICATE
    400: () => GlobalContext.l10n.err400, // Bad Request
    404: () => GlobalContext.l10n.err404, // Not Found
    405: () => GlobalContext.l10n.err405, // Method not allowed
  };
}
